import 'package:flutter/material.dart';

import 'loading_dialog.dart';

///Create Date:2022/1/28 13:52
///Author: Administrator

class LoadingDialogUtils{
  static LoadingDialogUtils get instance => _getInstance();

  static LoadingDialogUtils? _loadingDialogUtils;

  LoadingDialog? _loadingDialog;

  bool _isShowing=false;
  static LoadingDialogUtils _getInstance() {
    if (_loadingDialogUtils == null) {
      _loadingDialogUtils = new LoadingDialogUtils();
    }
    return _loadingDialogUtils!;
  }

  Route _initLoadingDialog(){
    return new LoadingDialog(cancelCallback: (){
      dismiss(isBackPress: true);
    },);
  }

  void showLoading(BuildContext? context){
    if (_isShowing == true) {
      return;
    }
    _loadingDialog=_initLoadingDialog() as LoadingDialog?;
    _isShowing=true;
    if(context==null){
      return;
    }
    _loadingDialog!.setBuildContext(context);
    Navigator.push(context,_loadingDialog!);
  }

  void dismiss({bool? isBackPress}){
    if ( _isShowing == true) {
      _isShowing = false;
      if(isBackPress==true){//按返回键返回
        return;
      }
      if(_loadingDialog?.context==null){
        return;
      }
      Navigator.of(_loadingDialog!.context).pop();
    }
  }
}